﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using DevExpress.XtraEditors;

namespace TMIS
{
    public partial class frmTeacher : XtraForm
    {
        private BLL.Teacher bll = new BLL.Teacher();
        public frmTeacher()
        {
            InitializeComponent();
            FillComponent();

        }
        #region 窗体控件数据初始化填充
        private void FillComponent()
        {
            comTitle.Properties.Items.AddRange(bll.GetTitle());
            comEducation.Properties.Items.AddRange(bll.GetEducation());
            this.teacherTableAdapter.Fill(this.reportDS.Teacher);
        }
        #endregion

        private void btnSave_Click(object sender, EventArgs e)
        {
            Model.Teacher tea = new Model.Teacher() 
            {
                ID=Guid.NewGuid(),
                tName=txtName.Text,
                tPassword=txtPassword.Text,
                tPhone=txtPhone.Text,
                tAddress=txtAddress.Text,
                tEmail=txtEmail.Text,
                tTitleID=bll.GetTitleID(comTitle.Text),
                tEducationID=bll.GetEducationID(comEducation.Text)
            };
            try
            {
                if (bll.Insert(tea))
                {
                    XtraMessageBox.Show("教师信息注册成功！");
                    this.teacherTableAdapter.Fill(this.reportDS.Teacher);
                }
                else 
                {
                    XtraMessageBox.Show("教师信息重复，注册失败！");
                }
            }
            catch (SqlException error)
            {
                #if DEBUG
                XtraMessageBox.Show(error.Message);
                #else
                XtraMessageBox.Show("教师信息注册失败");
                #endif
            }
        }

        private void btnDelTeacher_Click(object sender, EventArgs e)
        {
            try
            {
                //Guid guid = new Guid(this.TeacherView.SelectedRows[0].Cells[5].Value.ToString());
                Guid guid = new Guid(this.TeacherView.SelectedCells[0].OwningRow.Cells[5].Value.ToString());
                bll.Del(guid);
                XtraMessageBox.Show("教师信息删除成功！");
                this.teacherTableAdapter.Fill(this.reportDS.Teacher);
            }
            catch (SqlException error)
            {
                #if DEBUG
                XtraMessageBox.Show(error.Message);
                #else
                XtraMessageBox.Show("该教师正在指导学生论文，若要继续，请先移除所指导的论文信息！");
                #endif  
            }
        }

        private void frmTeacher_Load(object sender, EventArgs e)
        {

        }

    }
}
